package com.yc154.springmvc1.demos.web;

import org.apache.catalina.webresources.Cache;
import org.springframework.core.convert.converter.Converter;

import java.util.Calendar;
import java.util.Date;

public class DatteConvertor implements Converter<String, Date> {

    // 运行用户提交多种格式的日期值

    @Override
    public Date convert(String source) {
        String[] items = source.split("\\D+");
        int year = Integer.parseInt(items[0]);
        int month = 0;
        if(items.length >= 2){
            month = Integer.parseInt(items[1]);
        }
        int day = 0;
        if(items.length >= 3){
            day = Integer.parseInt(items[2]);
        }
        // 日历对象,用于时间运算
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, day);
        return calendar.getTime();
    }
}
